// pages/noAuth/index.js
const config = require("../../config/config.js")
const { LOGIN } = require("../../config/login.js")
const utils = require("../../utils/util")
const { Dialog, Notify, Toast } = require("../../config/modal")
const App = getApp()

Page({
    /** 页面的初始数据  */
    data: {
        isStartMp: App.$isStartMp(),
        capsuleBarHeight: App.$getMenuButtonHeight(),
        tab: {
            list: [
                {
                    name: "我的纸条"
                },
                {
                    name: "抽个对象"
                }
            ],
            activeKey: 1
        },
        sex: 1,
        schoolName: '',
        tab1: {
            list: [
                {
                    name: "我放入的纸条"
                },
                {
                    name: "我抽到的纸条"
                }
            ],
            activeKey: 0
        },
        swiperImgs: ['http://aliyun.thisforyou.cn:9004/cbd-prod/TuoDan/tuodan_01.jpeg', 'http://aliyun.thisforyou.cn:9004/cbd-prod/TuoDan/tuodan_02.jpeg'],
        listData: [],
        url: '/tuodan/myPush',
        showDoGetUser: false,
        page: {

        },
        config: {},
        userInfo: {}
    },
    URL: {
        myPush: "/tuodan/myPush",
        myGetList: "/tuodan/myGetList",
        delete: "/tuodan/delete/?",
        random: "/tuodan/random",
        toPay: "/tuodan/toPay",
        config: "/tuodan/config"
    },
    page: {
        push: '/pages/tuodan/push/index',
        schoolPage: "/pages/school/index",
    },

    /**
    * 生命周期函数--监听页面显示
    */
    onShow: function () {
        if (!App.$isAuth()) {
            App.$navigateTo('/pages/noAuth/index?nextPage=/pages/tuodan/index', false);
            return;
        }
        this.setData({
            isStartMp: App.$isStartMp(),
        })
        this.getConfig();
        //this.initData(this.URL.myPush)
    },

    getConfig() {
        App.$get(true, this.URL.config, {}, res => {
            if (res.R) {
                this.setData({
                    config: res.data
                })
            }
        })
    },


    /**
     * 生命周期函数--监听页面加载
     * "enablePullDownRefresh":true,
     */
    onLoad: function (options) {

        this.setData({
            userInfo: App.$getUserInfo(),
            schoolName: App.$getUserInfo().schoolName
        })
        // 检查是否被邀请进来的
        App.$handlerInviteCode(options);
    },

    initData(url) {
        this.setData({
            url: url
        })
        App.$get(true, url, {}, res => {
            if (res.R) {
                this.setData({
                    listData: res.data.rows,
                    'page.pages': res.data.pages,
                    'page.pageNum': res.data.pageNum,
                    'page.total': res.data.total
                })
            }
        })
    },

    delete(e) {
        let that = this;
        let code = utils.getEvent(e).item.tuoDanCode
        App.$alertConfirm({
            msg: '确定销毁？',
            success: function () {
                App.$delete(true, that.URL.delete.replace("?", code), {}, res => {
                    if (res.R) {
                        that.initData(that.URL.myPush)
                    }
                })
            }
        })
    }

    // 图片放大
    , showLagerImg(e) {
        let imgUrl = e.currentTarget.dataset.img;
        let imgUrls = e.currentTarget.dataset.imgs;
        wx.previewImage({
            current: imgUrl,
            urls: imgUrls
        })
    },

    /**
     * 点击抽取，拉取支付
     */
    toGetUser() {
        let _this = this;
        if (!this.data.config.pullStart) {
            App.$get(true, _this.URL.random, { sex: 2 }, res => {
                if (res.R) {
                    _this.setData({
                        'tab1.activeKey': 1,
                        'tab.activeKey': 0,
                        showDoGetUser: false
                    })
                    _this.initData(_this.URL.myGetList);
                    return;
                }
                _this.setData({
                    showDoGetUser: false
                })
            })
            return;
        }
        App.$post(true, this.URL.toPay, { schoolName: _this.data.schoolName, sex: _this.data.sex }, res => {
            if (res.R) {
                wx.requestPayment
                    (
                        {
                            "timeStamp": res.data.timeStamp,
                            "nonceStr": res.data.nonceStr,
                            "package": res.data.packageStr,
                            "signType": res.data.signType,
                            "paySign": res.data.paySign,
                            "success": function (res) {
                                console.log("吊起支付成功");
                                _this.setData({
                                    showDoGetUser: false
                                })
                                // 随机拿一个用户
                                App.$get(true, _this.URL.random, { schoolName: _this.data.schoolName, sex: _this.data.sex }, res => {
                                    if (res.R) {
                                        _this.setData({
                                            'tab1.activeKey': 1,
                                            'tab.activeKey': 0,
                                            showDoGetUser: false
                                        })
                                        _this.initData(_this.URL.myGetList)
                                    }
                                })
                            },
                            "fail": function (res) { console.log("吊起支付失败") },
                            "complete": function (res) { }
                        }
                    )
                return;
            }
            _this.setData({
                showDoGetUser: false
            })
        })
    },

    /**
     *  展示弹出框
     * @param {s} e 
     */
    drawOut(e) {
        let sex = utils.getEvent(e).sex;
        this.setData({
            sex: sex,
            showDoGetUser: true,
        })
    },

    onClosePopup() {
        this.setData({
            showDoGetUser: false,
        })
    },

    // tab栏切换
    onTypeChange(event) {
        let index = event.detail.index;
        this.setData({
            'tab.activeKey': index
        })
        if (index == 0) {
            this.initData(this.URL.myPush)
            return;
        }
    },
    onTypeChange1(event) {
        let index = event.detail.index;
        this.setData({
            'tab1.activeKey': index
        })

        if (index == 0) {
            this.initData(this.URL.myPush)
            return;
        }
        this.initData(this.URL.myGetList)
    },

    toPush(e) {
        let sex = utils.getEvent(e).sex;
        App.$navigateTo(this.page.push + "?sex=" + sex, false)
    },



    goHome() {
        App.$switchTab('/pages/tabbar/index/index', false);
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    // 获取当前所在位置
    toSelectSchoolName() {
        App.$navigateTo(this.page.schoolPage, false)
    },
    onSchoolNameChange(e) {
        this.setData({
            'schoolName': e.detail,
        })
    },
    clearSchool() {
        this.setData({
            schoolName: ''
        })
    },

    copyData(e) {
        let v = e.currentTarget.dataset.v;
        wx.setClipboardData({
            data: v,
            success: function (res) {
                wx.getClipboardData({
                    success: function () {
                        wx.showToast({
                            title: '已复制',
                            icon: 'none'
                        })
                    }
                })
            }
        })
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

        if (this.data.tab.activeKey != 0) {
            return;
        }

        if (this.data.page.pageNum + 1 > this.data.page.pages) {
            Toast("数据已全部加载")
            return;
        }

        App.$get(true, this.data.url, { pageNum: this.data.page.pageNum + 1 }, res => {
            if (res.R) {
                this.setData({
                    listData: res.data.rows,
                    'page.pages': res.data.pages,
                    'page.pageNum': res.data.pageNum,
                    'page.total': res.data.total
                })
            }
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return App.$onShare('/pages/tuodan/index', null, "校园脱单")

    }, toWaterMarkProgress() {
        wx.navigateToMiniProgram({
            appId: 'wxffe2cf21dabf0014',
            path: '',
            envVersion: 'release',// 打开正式版
            success(res) {
                // 打开成功
            },
            fail: function (err) {
                console.log(err);
            }
        })
    }
})
